package br.com.massai.controller.filter;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

import br.com.massai.view.enums.PerfilEnum;
import br.com.massai.view.utils.NavigationUtil;

@WebFilter("/AdminFilter")
public class AdminFilter implements Filter {

	@Inject private SessionControl session;

    public AdminFilter() { }

	public void destroy() { }

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		if(session.loggedIn()){
			if(session.perfilLogado().equals(PerfilEnum.A))
				chain.doFilter(request, response);
			else
				((HttpServletResponse) response).sendRedirect(NavigationUtil.TO_HOME);

		} else {
			((HttpServletResponse) response).sendRedirect(NavigationUtil.TO_HOME);
		}
	}

	public void init(FilterConfig fConfig) throws ServletException { }
}